package zmee.pur.src.common;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.AbstractOperate;
import kd.scm.common.constant.BillAssistConstant;
import kd.scm.pds.common.comptpl.PdsCompTplContext;
import kd.scm.pds.common.comptpl.PdsCompTplServiceImpl;
import kd.scm.pds.common.constant.SrcCommonConstant;
import kd.scm.pds.common.constant.SrcMetadataConstant;
import kd.scm.pds.common.constant.SrcOperationConstant;
import kd.scm.pds.common.util.PdsCommonUtils;

/**
 * 资审后审 组件化插件实现--浙江物产
 *
 * @author rd_rengui_chen
 */
public class SrcAptitudeAudit2CompTplServiceImpl_zjwc extends PdsCompTplServiceImpl {
    @Override
    public void beforeDoOperation(PdsCompTplContext context) {
        initParam(context);
        BeforeDoOperationEventArgs beforeDoOperation = context.getBeforeDoOperationEventArgs();
        String opKey = ((AbstractOperate) beforeDoOperation.getSource()).getOperateKey();
        switch (opKey) {
            // 开标、下达资审任务、综合计算
            case SrcCommonConstant.ALLOPEN:
            case SrcOperationConstant.PUSHAPTITUDE:
            case SrcOperationConstant.RECALCULATE:
                // 先保存插件数据
                boolean isSuccess = doOperation(BillAssistConstant.OP_SAVE);
                if (!isSuccess) {
                    beforeDoOperation.setCancel(true);
                }
                break;
            default:
                super.beforeDoOperation(context);
        }
    }

    @Override
    public void propertyChanged(PdsCompTplContext context) {
        initParam(context);
        PropertyChangedArgs e = context.getPropertyChangeArgs();
        String key = e.getProperty().getName();
        switch (key) {
            // 资审供应商来源
            case SrcCommonConstant.SUPPLIERTYPE:
                // 资审方式
            case SrcCommonConstant.APTITUDETYPE:
                Object template = this.getModel().getValue(SrcCommonConstant.TEMPLATE);
                if (template != null) {
                    DynamicObject templateObj = (DynamicObject) template;
                    super.clearComponents();
                    // 根据模板获取组件
                    this.loadComponents(templateObj);
                }
                break;
            default:
                break;
        }
        super.propertyChanged(context);
    }

    @Override
    public boolean isShowComponent(String compKey) {
        switch (compKey) {
            // 回标详情、回标信息、回标情况、快速采购回标信息、弃标的情况
            case SrcMetadataConstant.SRC_BIDOPEN_TENDER:
            case SrcMetadataConstant.SRC_TENDERINFO_SUMMARY:
            case SrcMetadataConstant.SRC_TENDER_QUICK:
            case SrcMetadataConstant.SRC_TENDERINFO:
            case SrcMetadataConstant.SRC_BIDDISCARD:
                return PdsCommonUtils.isShowTenderBeforeBidOpen(this.getModel().getDataEntity());
            // 开标情况组件
            case SrcMetadataConstant.SRC_BIDOPEN_OPEN:
                return !PdsCommonUtils.isOpenBySupplier(this.getModel().getDataEntity());
            // 按供应商开标情况组件
            case SrcMetadataConstant.SRC_SUPPLIER_OPEN:
                return PdsCommonUtils.isOpenBySupplier(this.getModel().getDataEntity());

            default:
                return super.isShowComponent(compKey);
        }
    }
}